﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.Sqlite;
using System.Data;
using System.Data.Common;

namespace Terminal
{

    public delegate void output(string stroutput);
    public class BarcodeRecords
    {
        public DateTime time { get; set; }
        public string barcode { get; set; }
    }
    public class BarcodeRecordsAccess
    {
        public output outputHandler; 
        readonly string DBPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "database", "BarcodeRecords.DB");
        static IDbConnection _db = null;
        public BarcodeRecordsAccess()
        {
            if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(DBPath)))
            {
                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(DBPath));
            }
            var dbfactory = new OrmLiteConnectionFactory(DBPath, SqliteDialect.Provider);
            _db = dbfactory.Open();
            if (!_db.TableExists<BarcodeRecords>())
            {
                _db.CreateTable<BarcodeRecords>();
            }
        }

        public void output(string output)
        {
            outputHandler?.Invoke(output);
        }
        /// <summary>
        /// 插入条毛
        /// </summary>
        /// <param name="Barcode"></param>
        public void Insert(string Barcode)
        {
            _db.Insert(new BarcodeRecords() { time = DateTime.Now, barcode = Barcode });
        }
        /// <summary>
        /// 检查是否有效
        /// </summary>
        /// <param name="Barcode"></param>
        /// <returns></returns>
        public int ValidChecking(string Barcode)
        {
            int ret =0;
            var expression = _db.From<BarcodeRecords>().Where(t => t.barcode == Barcode).OrderBy(t => t.time);
            var q = _db.Select(expression);
            if (q != null && q.Count > 0)
            {
                try
                {
                    var endtime = new DateTime(int.Parse(Barcode.Substring(21, 4)), int.Parse(Barcode.Substring(25, 2)), int.Parse(Barcode.Substring(27, 2)));
                    if (DateTime.Now > endtime)
                    {                      
                        ret = -1;
                    }
                    else if (DateTime.Now > q.First().time.AddDays(30))
                    {
                        ret = -2;
                    }
                    output("开封日期:" + q.First().time.ToString("yyyy-MM-dd HH:mm:dd")+Environment.NewLine);
                    output("截止日期:" + endtime.ToString("yyyy-MM-dd HH:mm:dd")+Environment.NewLine);
                    output("开封失效日期" + q.First().time.AddDays(30).ToString("yyyy-MM-dd HH:mm:dd") + Environment.NewLine);
                    output("该条码已使用时间" + ( q.Last().time- q.First().time).TotalDays.ToString("f2") + "天" + Environment.NewLine);
                    output("距离截止时间还剩" + (endtime - DateTime.Now).TotalDays.ToString("f2") + "天" + Environment.NewLine);
                }
                catch
                { 
                    ret = -4;
                }
            }
            else
            {
                ret= -3;
            }
            return ret;
        }
    }

}
